<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <dept-tree :node-dept="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>
        <!-- 下面内容 -->
        <el-tree
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template #default="{data}">
            <!-- 树形 -->
            <dept-tree
              :node-dept="data"
              @delDepartment="getDepartments"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <!-- 弹窗 -->
      <adddept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node-dept="nodeDept"
        @addDepartment="getDepartments"
      />
    </div>
  </div>
</template>

<script>
// 引入 树形组件
import deptTree from './components/dept-tree.vue'
// 引入 弹窗组件
import adddept from './components/add-dept.vue'
// 部门列表
import { getDepartments } from '@/api/departments'
// 引入封装递归 查找children
import { listToTreeData } from '@/utils'
export default {
  // 注册tree组件
  components: { deptTree, adddept },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      // 部门数据
      depts: [],
      showDialog: false,
      nodeDept: {}
    }
  },
  // 表单数据请求
  created() {
    this.getDepartments()
  },
  // 表单数据请求
  methods: {
    async getDepartments() {
      // 部门数据请求
      const { depts } = await getDepartments()
      this.depts = listToTreeData(depts, '')
    },
    // 父组件接收传nodeDept 以及弹窗（添加）
    addDepartment(nodeDept) {
      // 弹窗
      this.showDialog = true
      // 赋值nodeDept
      this.nodeDept = nodeDept
    },
    // 父组件接收传nodeDept 以及弹窗 (编辑)
    async editDepartment(nodeDept) {
      // 获取弹窗的数值传入匹配id
      await this.$refs.addDept.getDepartmentDetail(nodeDept.id)
      // 弹窗
      this.showDialog = true
      // 赋值nodeDept
      this.nodeDept = nodeDept
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
